#pragma once
#include "stdafx.h"
#include <windows.h>
#include <shlobj.h>
#include <string>

#define CCFileConvert L"c:\\lala.txt"

using namespace std;

// The CConvert class mirrors the Convert class that is 
// defined in the .NET Framework. It converts primitives 
// and other data types to wstring types.
class CConvert
{
public:
    // The ToWString method converts a HANDLE to a wstring.
    // h: the HANDLE to convert to a wstring.
    // return: the HANDLE as a wstring.
    static wstring ToWString(HANDLE h)
    {
        // If the HANDLE is NULL, return the "NULL" string.
        if (NULL == h)
        {
            return L"NULL";
        }

        // If the HANDLE is not valid, return 
        // the INVALID_HANDLE_VALUE as a string.
        if (INVALID_HANDLE_VALUE == h)
        {
            return L"INVALID_HANDLE_VALUE";
        }

        // The HANDLE is valid.
        return L"valid";
    }

	static void CConvertToDuplicator(LPSYSTEMTIME dup1, double* dup2)
	{
		SystemTimeToVariantTime(dup1, dup2);
	}

    // The ToWString method converts a FILETIME pointer to a wstring.
    // fileTime: the FILETIME pointer to convert to a wstring.
    // return: the FILETIME pointer as a wstring.
    static wstring ToWString(FILETIME* fileTime)
    {
        // If fileTime is NULL, return the empty string.
        if (NULL == fileTime)
        {
            return L"NULL";
        }

        // Convert the FILETIME to a local time, and 
        // then convert that local time to a wstring.
        SYSTEMTIME stUTC;
        //SYSTEMTIME stLocal;
        FileTimeToSystemTime(fileTime, &stUTC);
        //SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);        

        // Create a wstring to return.  Note: wsprintf 
        // can be also used. However, it is more difficult 
        // to handle both UNICODE and non-UNICODE correctly.
        wstring timeString =
            ToWString(stUTC.wDay) +
            wstring(L"/")            +
            ToWString(stUTC.wMonth)   +
            wstring(L"/")            +
            ToWString(stUTC.wYear)  +
            wstring(L"  ")           +
            ToWString(stUTC.wHour)  +
            wstring(L":")            +
            ToWString(stUTC.wMinute);

        // Return the FILETIME data as a wstring.
        return timeString;
    }

	static string ToString(FILETIME* fileTime)
	{
		// If fileTime is NULL, return the empty string.
		if (NULL == fileTime)
		{
			return "NULL";
		}

		// Convert the FILETIME to a local time, and 
		// then convert that local time to a wstring.
		SYSTEMTIME stUTC;
		//SYSTEMTIME stLocal;
		FileTimeToSystemTime(fileTime, &stUTC);
		//SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);        

		// Create a wstring to return.  Note: wsprintf 
		// can be also used. However, it is more difficult 
		// to handle both UNICODE and non-UNICODE correctly.
		string timeString =
			ToString((long)stUTC.wDay) +
			string("/")            +
			ToString((long)stUTC.wMonth)   +
			string("/")            +
			ToString((long)stUTC.wYear)  +
			string("  ")           +
			ToString((long)stUTC.wHour)  +
			string(":")            +
			ToString((long)stUTC.wMinute);

		// Return the FILETIME data as a wstring.
		return timeString;
	}

    // The ToWString method converts a 
    // ULARGE_INTEGER pointer to a wstring.
    // ui: the ULARGE_INTEGER pointer to convert to a string.
    // return: the ULARGE_INTEGER pointer as a string.
    static wstring ToWString(ULARGE_INTEGER* ui)
    {
        // Return the empty string if the 
        // ULARGE_INTEGER pointer is NULL.
        if (NULL == ui)
        {
            return L"NULL";
        }

        // Return the low-order part to a wstring.
        return (ToWString(ui->LowPart));
    }

    // The ToWString method converts a PCSTR to a wstring.
    // pcstr: the PCSTR to convert to a wstring.
    // return: the PCSTR as a wstring.
    static wstring ToWString(PCSTR pcstr)
    {
        // Return the empty string 
        // if the PCSTR is NULL.
        if (NULL == pcstr)
        {
            return L"NULL";
        }

        // Get the length of the string to copy.
        size_t length = strlen(pcstr);
        // Create a new double-byte character 
        // array of length plus 1.
        wchar_t* newText = new wchar_t[length+1];

        // Copy the source into the sink string.
        for (size_t i = 0; i < length; ++i)
        {
            newText[i] = pcstr[i];
        }

        // Terminate the string with the NULL character.
        newText[length] = '\0';
        // Get a wstring from the new double-byte string.
        wstring wText = newText;
        // Call delete on the newText pointer 
        // and set this pointer to NULL.
        delete[] newText;
        newText = NULL;

        // Return the wstring copy.
        return wText;
    }

    // The ToWString method converts a long to a wstring.
    // l: the long value to convert to a wstring.
    // return: the long as a wstring.
    static wstring ToWString(long l)
    {
        WCHAR str[256];
        str[0] = '\0';       
		_ltow_s(l, str, 256, 10);
        return str;
    }

	static string ToString(long l)
	{
		CHAR str[256];
		str[0] = '\0';        
		_ltoa_s(l, str, 256, 10);
		return str;
	}

    // The ToWString method converts a DWORD to a wstring.
    // d: the DWORD value to convert to a wstring.
    // return: the DWORD as a wstring.
    static wstring ToWString(DWORD d)
    {
        return ToWString((long)d);
    }

    // The ToWString method converts an LPCGUID to a wstring.
    // guid: the LPCGUID value to convert to a wstring.
    // return: The LPCGUID as a wstring; otherwise, L"" if
    // guid is NULL.
    static wstring ToWString(LPCGUID guid)
    {
        // If the GUID is NULL, return the empty string.
        if (NULL == guid)
        {
            return L"NULL";
        }

        // Create a WCHAR array to write the GUID to.
        WCHAR guidString[256];
        // Initialize the zero-based index of the 
        // guidString to the null-terminating character 
        // because the StringFromGUID2 may fail.
        guidString[0] = '\0';
        // Convert the GUID to a string of the form "{...}".
        //int characters = StringFromGUID2(*guid, guidString, 256);
        // Return the guidString as a wstring.
        return guidString;
    }

    // The ToWString method converts a BOOL to a wstring.
    // b: the BOOL value to convert to a wstring.
    // return: L"true" for true; otherwise, L"false" for false.
    static wstring ToWString(BOOL b)
    {
        return (b) ? L"true" : L"false";
    }

    // The ToByteString converts a double-byte 
    // character string to a single-byte string.
    // str: the double-byte string to convert.
    // return: a single-byte string copied from str.
    static string ToByteString(const wstring& str)
    {
        // Get the length of the 
        // double-byte string.
        size_t length = str.length();

        // Create a temporary char pointer.
        char* byteChar = new char[length+1];
        byteChar[0] = '\0';
        // Copy the double-byte character string
        // into the single-byte string.        
        size_t charsReturned = 0;
        wcstombs_s(&charsReturned, byteChar, 
                   length+1, str.c_str(), length+1);
        // Create a string to return.
        string retString = byteChar;
        // Delete the temporary string and
        // set that string to NULL.
        delete[] byteChar;
        byteChar = NULL;

        // Return the single-byte string.
        return retString;
    }

    // The ToWString method converts a USHORT to a wstring.
    // u: the USHORT value to convert to a wstring.
    // return: the value of u as a wstring.
    static wstring ToWString(USHORT u)
    {
        return (ToWString((long)u));
    }

    // The ToWString method converts a 
    // const BYTE pointer to a wstring.
    // bytes: the BYTE pointer to convert.
    // return: the value of bytes as a wstring.
    static wstring ToWString(const BYTE* bytes)
    {
        return (ToWString((PCSTR)bytes));    
    }

    // The ToWString method converts 
    // a PCWSTR to a wstring.
    // pcwstr: the PCWSTR to convert.
    // return: L"NULL" if the pcwstr
    // parameter is NULL; otherwise, 
    // pcwstr converted to a wstring.
    static wstring ToWString(PCWSTR pcwstr)
    {
        // If the pcwstr parameter
        // is NULL, return L"NULL".
        if (NULL == pcwstr)
        {
            return L"NULL";
        }

        // Implicitly convert the 
        // PCWSTR to a wstring.
        return pcwstr;
    }
};